<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/6/20
 * Time: 11:25
 */
namespace app\admin\controller;
use think\Db;
class Fund extends Base{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(session ('area')){
                $map['fund_qu_id'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['fund_title|user_username'] = ['like',"%" . $key . "%"];
            }
            $map['fund_status'] = ['neq',6];
            if(isset($status)&&$status != ""){
                $map['fund_status'] = $status;
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('fund f')
                ->join('u_user u','u.user_id = f.fund_user_id','left')
                ->join('area a','a.district_id = f.fund_qu_id','left')
                ->where($map)
                ->count();
            $lists =  Db::name('fund f')
                ->join('u_user u','u.user_id = f.fund_user_id','left')
                ->join('area a','a.district_id = f.fund_qu_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('fund_add_time desc')
                ->select ();
            if(!empty($lists)){
                foreach ($lists as $key=>$vo){
                    $lists[$key]['fund_imgs'] = explode (',',$vo['fund_imgs']);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(session("?fund_id")){
            session('fund_id',null);
        }
        return $this->fetch('fund/index');
    }


    public function doCheck(){
        extract (input());
        $param = [
            'fund_status'=>$type,
            'fund_edit_time'=>date('Y-m-d H:i:s')
        ];
        if($type == 2){
            $param['fund_check_time'] = date('Y-m-d H:i:s');
        }
        $res = Db::name('fund')->where('fund_id',$fund_id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'审核成功']);
        }else{
            return json(['code'=>100,'msg'=>'审核失败']);
        }
    }

    public function look(){
        extract (input());
        if(request()->isAjax ()){
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['user_phone'] = ['like',"%" . $key . "%"];
            }
            $map['j_fund_id'] = session('fund_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('fund_join f')
                ->join('u_user u','f.j_user_id = u.user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('fund_join f')
                ->join('u_user u','f.j_user_id = u.user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('j_pay_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        session('fund_id',$id);
        return $this->fetch ('fund/look_fund');
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelAdmin(){
        extract(input());
        if($key == ""){
            $data = Db::name('fund_join f')
                ->join('u_user u','f.j_user_id = u.user_id','left')
                ->select();
        }else{
            $map = [];
            if($key!="")
            {
                $map['user_phone'] = ['like',"%" . $key . "%"];
            }
            $data = Db::name('fund_join f')
                ->join('u_user u','f.j_user_id = u.user_id','left')
                ->where($map)
                ->select();
        }
        $cellname = [
            ['j_id','ID',15,'LEFT'],
            ['user_username','用户名',15,'LEFT'],
            ['user_phone','手机号',15,'LEFT'],
            ['j_pay','金额',20,'LEFT'],
            ['j_pay_time','众筹时间',15,'LEFT']
        ];
        $res = exportExcel('参与众筹用户列表','fund',$cellname,$data);
        return json($res);
    }

    public function country(){
        extract (input());
        $res = Db::name('fund')->where('fund_id',$id)->update(['fund_country'=>$num,'fund_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json([]);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }

}